import ListItem from "./ListItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Home extends cc.Component {
    @property(cc.ScrollView)
    private gameList: cc.ScrollView = null;
    @property(cc.Node)
    private item:cc.Node;

    @property(cc.Node)
    private content:cc.Node=null;
    private count:number=20;
    private space:number=5;
    private itemHeight:number=80;
    private gameData:Array<string>=["地图","角色","背包","商城","战斗"];
    private sceneDict:Array<string>=["Maps","Player","bag","shop","war"];

    onLoad(){
        this.count=this.gameData.length;
        this.content=this.gameList.content;
        this.content.height=this.count*(this.itemHeight+this.space);
        for(let i:number=0;i<this.count;i++){
            let item=cc.instantiate(this.item);
            this.content.addChild(item);
            item.setPosition(0,-item.height*(0.5+i)-this.space*(i+1));
            item.getComponent("ListItem").onUpdate(this.gameData[i],i);
            item.on(cc.Node.EventType.TOUCH_END,this.itemClick,this);
        }
    }

    itemClick(e){
        let node:ListItem=e.target.getComponent("ListItem");
        console.log("click "+node.label.string);
        this.loadSceneWithProgress(this.sceneDict[node.index]);
    }

    start () {

    }
    private loadSceneWithProgress(scene:string){
        cc.director.loadScene(scene);
    }
}
